import requests
from bs4 import BeautifulSoup
import json
from seasun.datasoures.xiaohua_config import insertxiaohua

url = 'http://www.jokeji.cn'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}

def myRequest(url: str, headers:str, select:str) ->str:
    '''
    自定义请求函数
    :param url:
    :param headers:
    :param select:
    :return:
    '''
    strhtml = requests.get(url, headers=headers)
    strhtml.encoding = 'gbk'
    soup = BeautifulSoup(strhtml.text, 'lxml')
    # select选择器: class(.)    id(#)    标签(ul)
    data = soup.select(select)
    return data

def firstpage(index: int) ->list:
    '''
    获取最新笑话列表链接
    :return:
    '''
    print('第' + str(index) + '页')
    page = '/list_' + str(index) + '.htm'
    select = '.list_title>ul>li>b>a'
    data = myRequest(url + page, headers, select)
    listA = []
    listText = []
    for item in data:
        listA.append(url + item['href'])
        listText.append(item.text)
    return listA, listText

def seconds(index: int)-> None:
    '''
    使用获取的连接请求得到笑话数据
    :return:
    '''
    urls, listTitle = firstpage(index)
    select = '.main>.left>.left_up>ul>span>p'
    for url, ltitle in zip(urls, listTitle):
        data = myRequest(url, headers, select)
        item = ''
        for text in data:
            item = item + '\n' + text.text
        insertxiaohua(ltitle, item)
        print('插入成功')
if __name__ == '__main__':
    for i in range(6, 201):
        seconds(i)